'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 在线用户数统计信息
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var modelUtil = require('../util/modelUtil');
var mongoConnector = require('../libs/mongodb/mongoConnector');
/* jshint unused:false */
var onlineUserCountSchema = require('../schemas/onlineUserCountSchema');
var OnlineUserCount = mongoConnector().model('OnlineUserCount');

/**
 * 查询所有
 */
module.exports.findAll = function (cb) {
    OnlineUserCount.find({}, cb);
};

/**
 * 按渠道查询
 */
module.exports.findByChannel = function (channel_id, cb) {
    // 创建成功后
    function afterCreate(err, schema) {
        cb(err, schema);
    }

    // 查询成功后
    function afterFindOne(err, schema) {
        if (!!err) {
            return cb(err);
        }
        if (!!schema) {
            return cb(null, schema);
        } else {
            OnlineUserCount.create({
                channel_id: channel_id
            }, afterCreate);
        }
    }

    // 查询
    OnlineUserCount.findOne({
        channel_id: channel_id
    }, afterFindOne);
};

/**
 * 按渠道删除
 */
module.exports.removeByChannel = function (channel_id, cb) {
    OnlineUserCount.remove({
        channel_id: channel_id
    }, cb);
};

/**
 * 清除在线人数信息
 */
module.exports.clearAll = function (cb) {
    OnlineUserCount.remove(null, cb);
};
